package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.common.context.BaseContext;
import com.heima.common.result.PageResult;
import com.heima.mapper.VmMapper;
import com.heima.pojo.dto.VmDTO;
import com.heima.pojo.entity.VendingMachine;
import com.heima.pojo.vo.NodeDetailVO;
import com.heima.service.VendingMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


/**
 * \* User: chy
 * \* Date: 2025/8/22  14:10
 * \* Description:
 * \
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VmMapper, VendingMachine> implements VendingMachineService {
    @Autowired
    private VmMapper vmMapper;

    /**
     * 设备搜索
     * @param vmDTO
     * @return
     */
    @Override
    public PageResult<NodeDetailVO> pageResult(VmDTO vmDTO) {

        //创建分页对象
        Page<NodeDetailVO> page = new Page<>(vmDTO.getPageIndex(), vmDTO.getPageSize());

        //执行分页查询
        IPage<NodeDetailVO> resultPage = vmMapper.selectVmDetail(page, vmDTO.getInnerCode());

        return PageResult.<NodeDetailVO>builder()
                .pageIndex(vmDTO.getPageIndex())//当前页码
                .pageSize(vmDTO.getPageSize())//每页记录数
                .totalPage((int) resultPage.getPages())//总页数
                .totalCount((int) resultPage.getTotal())//总记录数
                .currentPageRecords(resultPage.getRecords())
                .build();
    }

    /**
     * 新增设备
     * @param vendingMachine
     * @return
     */
    @Override
    public boolean saveVm(VendingMachine vendingMachine) {

        Boolean result = super.save(vendingMachine);
        return result;
    }

    /**
     * 修改设备节点
     * @param VmId
     * @param nodeId
     * @return
     */
    @Override
    public boolean updateVmNode(Long VmId, Long nodeId) {

        //创建查询条件
        VendingMachine vendingMachine = new VendingMachine();
        vendingMachine.setId(VmId);
        vendingMachine.setNodeId(nodeId);

        return super.updateById(vendingMachine);
    }
}
